package cn.wnhyang.solon.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.wnhyang.solon.vo.TimeVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.noear.snack.ONode;
import org.noear.snack.core.Options;
import org.noear.solon.annotation.*;
import org.noear.solon.serialization.jackson.JacksonActionExecutor;
import org.noear.solon.serialization.snack3.SnackActionExecutor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author wnhyang
 * @date 2025/5/3
 **/
@Slf4j
@Mapping("/demo")
@Controller
public class DemoController {

    @Inject
    private SnackActionExecutor snackActionExecutor;

    @Inject
    private JacksonActionExecutor jacksonActionExecutor;

    private static Options options;

    private static ObjectMapper objectMapper;

    @Init
    public void init() {
        options = snackActionExecutor.config();
        objectMapper = jacksonActionExecutor.config();
    }

    @Post
    @SaCheckLogin
    @Mapping("/now")
    public TimeVO now() {
        return new TimeVO().setId(1L).setLocalTime(LocalTime.now()).setLocalDate(LocalDate.now()).setLocalDateTime(LocalDateTime.now());
    }

    @Post
    @Mapping("/now1")
    public TimeVO now1(@Body TimeVO timeVO) {
        log.info("now1:{}", timeVO);
        return timeVO;
    }

    @Post
    @Mapping("/now2")
    public TimeVO now2(@Body TimeVO timeVO) {
        log.info("now2:{}", timeVO);
        String json = ONode.loadObj(timeVO, options).toJson();
        log.info("now2:{}", json);
        Object object = ONode.loadStr(json, options).toObject(TimeVO.class);
        log.info("now2:{}", object);
        return timeVO;
    }

    @Post
    @Mapping("/now3")
    public TimeVO now3(@Body TimeVO timeVO) throws JsonProcessingException {
        log.info("now3:{}", timeVO);
        String json = objectMapper.writeValueAsString(timeVO);
        log.info("now3:{}", json);
        Object object = objectMapper.readValue(json, TimeVO.class);
        log.info("now3:{}", object);
        return timeVO;
    }
}
